package com.world.tg.exception;

import com.world.tg.result.BaseResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionController {

    @ResponseBody
    @ExceptionHandler(UnknownAccountException.class)
    public BaseResult doShiroException(ShiroException e){
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(200);
        if(e instanceof UnknownAccountException){
            baseResult.setMsg("用户名不存在");
        }else if(e instanceof IncorrectCredentialsException){
            baseResult.setMsg("密码不正确");
        }else if(e instanceof LockedAccountException){
            baseResult.setMsg("用户被锁定");
        }else if(e instanceof AuthorizationException){
            baseResult.setMsg("没有权限");
        }else {
            baseResult.setMsg("认证或授权失败");
        }
        return baseResult;
    }
}
